package com.pengpeng.sheepfoldmall.coupon.service.impl;

import com.pengpeng.common.to.MemberPrice;
import com.pengpeng.common.to.SkuReductionTo;
import com.pengpeng.sheepfoldmall.coupon.entity.MemberPriceEntity;
import com.pengpeng.sheepfoldmall.coupon.entity.SkuLadderEntity;
import com.pengpeng.sheepfoldmall.coupon.service.MemberPriceService;
import com.pengpeng.sheepfoldmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpeng.common.utils.PageUtils;
import com.pengpeng.common.utils.Query;

import com.pengpeng.sheepfoldmall.coupon.dao.SkuFullReductionDao;
import com.pengpeng.sheepfoldmall.coupon.entity.SkuFullReductionEntity;
import com.pengpeng.sheepfoldmall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku的优惠、满减等详细信息
     * @param skuReductionTo
     */
    @Override
    public void saveSkuReductionInfo(SkuReductionTo skuReductionTo) {
        //保存sku的优惠信息（阶梯价格）
        //有优惠情况再去保存相关信息
        if (skuReductionTo.getFullCount() > 0){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
            skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
            skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        //保存满减信息
        //有满减信息再去保存数据
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(skuReductionTo.getCountStatus());
            this.save(skuFullReductionEntity);
        }

        //保存会员价格
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().filter(item -> {
            //先过滤，没有填写会员价格就不需要保存数据库
            return item.getPrice().compareTo(new BigDecimal("0")) == 1;
        }).map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);  //设置默认叠加其他优惠
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }
}